# -*- coding: utf-8 -*-
# @Author  : CaoHan
# @Time    : 2022/11/15 10:02
from ds.enumClass import ContainerState

containerId = 0
AppMem = 15  # GB
AppFlops = 10  # G FLOPS


class TimeSlot:
    def __init__(self, start):
        self.start = start
        self.end = -1


class Container:
    def __init__(self, uavId, createTime):
        global containerId
        self.id = containerId
        containerId += 1
        self.mem = AppMem
        self.flops = AppFlops
        self.coldStartTime = 10  # s
        self.uavId = uavId
        self.state = ContainerState.COLD_START
        self.createTime = createTime
        self.killedTime = -1
        self.runTime = []
        self.csTime = []
        self.csTime.append(TimeSlot(createTime))

    def run(self, time):
        # if self.state == ContainerState.RUN:  # queue + reuse
        #     do nothing
        if self.state == ContainerState.COLD_START:  # run
            self.csTime[-1].end = time
            self.runTime.append(TimeSlot(time))
        elif self.state == ContainerState.SPARE:  # reuse
            self.runTime.append(TimeSlot(time))
            self.killedTime = -1
        self.state = ContainerState.RUN

    def spare(self, time, reuseTimeWindow):
        self.killedTime = time + reuseTimeWindow
        self.runTime[-1].end = time
        self.state = ContainerState.SPARE

    def kill(self):
        self.state = ContainerState.KILL
